var express = require('express')
var router = express.Router()
var userService = require('../../../../services/UserService')

// //验证模块
// var authorization=require(path.join(process.cwd(),"/modules/authorization"))

// //通过验证模块获取用户管理服务
// var mgrServ=authorization

//查询用户列表
router.get('/', (req, res, next) => {
    if (!req.query.pagenum || req.query.pagenum <= 0) return res.sendResult(null, 400, "pagenum 参数错误");
    if (!req.query.pagesize || req.query.pagesize <= 0) return res.sendResult(null, 400, "pagesize 参数错误");
    next();
},
    (req, res, next) => {
        console.log(req.query.query)
        userService.getAllUsers(
            {
                "query": req.query.query,
                "pagenum": req.query.pagenum,
                "pagesize": req.query.pagesize
            },
            function (err, result) {
                if (err) return res.sendResult(null, 400, err);
                res.sendResult(result, 200, "获取管理员列表成功");
            }
        )
    }
)

//获取用户信息
router.get('/:id', (req, res, next) => {
    if (!req.params.id) {
        return res.sendResult(null, 400, "用户ID不能为空");
    }
    if (isNaN(parseInt(req.params.id))) return res.sendResult(null, 400, "用户ID必须是数字");
    next();
},
    (req, res, next) => {
        userService.getUser(req.params.id, (err, user) => {
            if (err) return res.sendResult(null, 400, err);
            res.sendResult(user, 200, "获得成功");
        })
    }
)

//创建用户
router.post('/', (req, res, next) => {
    // 验证参数

    if (!req.body.username) {
        return res.sendResult(null, 400, "用户名不能为空");
    }
    if (!req.body.password) {
        return res.sendResult(null, 400, "密码不能为空");
    }
    if (!req.body.rid) {
        req.body.rid = -1;
        //return res.send(null,200,"角色ID不能为空");
    }
    if (isNaN(parseInt(req.body.rid))) req.body.rid = -1;//return res.send(null,200,"角色ID必须是数字");
    next();
},
    (req, res, next) => {
        params = {
            "username": req.body.username,
            "password": req.body.password,
            "mobile": req.body.mobile,
            "email": req.body.email,
            "rid": req.body.rid
        }
        userService.createUser(params, (err, user) => {
            if (err) return res.sendResult(null, 400, err);
            res.sendResult(user, 201, "创建成功");
        })
    }
)

//修改用户信息
router.put('/:id', (req, res, next) => {
    if (!req.params.id) {
        return res.sendResult(null, 400, "用户ID不能为空");
    }
    if (isNaN(parseInt(req.params.id))) return res.sendResult(null, 400, "用户ID必须是数字");
    next();
},
    (req, res, next) => {
        userService.updateUser(
            {
                "id": req.params.id,
                "mobile": req.body.mobile,
                "email": req.body.email
            },
            (err, user) => {
                if (err) return res.sendResult(null, 400, err);
                res.sendResult(user, 200, "更新成功");
            }
        )
    }
)

//删除用户信息
router.delete('/:id', (req, res, next) => {
    if (!req.params.id) return res.sendResult(null, 400, "用户ID不能为空");
    if (isNaN(parseInt(req.params.id))) return res.sendResult(null, 400, "ID必须是数字");
    if (req.params.id == 500) return res.sendResult(null, 400, "不允许删除admin账户");
    next();
},
    (req, res, next) => {
        userService.deleteUser(req, params.id, (err) => {
            if (err) return res.sendResult(null, 400, err);
            return res.sendResult(null, 200, "删除成功");
        })
    }
)

//变更用户角色
router.put('/:id/role', (req, res, next) => {
    if (!req.params.id) {
        return res.sendResult(null, 400, "用户ID不能为空");
    }
    if (isNaN(parseInt(req.params.id))) return res.sendResult(null, 400, "用户ID必须是数字");

    if (req.params.id == 500) return res.sendResult(null, 400, "不允许修改admin账户");

    if (!req.body.rid) res.sendResult(null, 400, "权限ID不能为空");
    next();
},
    (req, res, next) => {
        userService.setRole(req.params.id, req.body.rid, (err, user) => {
            if (err) return res.sendResult(null, 400, err);
            res.sendResult(manager, 200, "设置角色成功");
        })
    }
)

//设置用户状态
router.put('/:id/state/:state', (req, res, next) => {
    if (!req.params.id) {
        return res.sendResult(null, 400, "用户ID不能为空");
    }
    if (isNaN(parseInt(req.params.id))) return res.sendResult(null, 400, "用户ID必须是数字");
    next();
},
    (req, res, next) => {
        state = 0
        if (req.params.state && req.params.state == "true") state = 1
        userService.updateUserState(req.params.id, state, function (err, user) {
            if (err) return res.sendResult(null, 400, err);
            res.sendResult(user, 200, "设置状态成功");
        })
    }
)

module.exports = router